//
//  MidiWindow.swift
//  helloworld
//
//  Created by Mike on 2016/10/29.
//  Copyright © 2016年 GindaChen. All rights reserved.
//

import Cocoa
import CoreMIDI
import CoreMedia
import AVFoundation

enum keyboardOperationKey : Character {
    case
        keyboardUpLeft = "q",
        keyboardUpRight = "p",
    
        keyboardDownLeft = "z",
        keyboardDownRight = "/"
}

class MidiViewControllerBase: NSViewController {
    
    var keyboardEvent : NSEvent
//    var player : AVMIDIPlayer
    
    //The First  row
    @IBOutlet weak var uleft: MidiKeyButton!
    @IBOutlet weak var uright: MidiKeyButton!
    
    @IBOutlet weak var uw: MidiKeyButton!
    @IBOutlet weak var u3: MidiKeyButton!
    @IBOutlet weak var ue: MidiKeyButton!
    @IBOutlet weak var u4: MidiKeyButton!
    @IBOutlet weak var ur: MidiKeyButton!
    @IBOutlet weak var ut: MidiKeyButton!
    @IBOutlet weak var u6: MidiKeyButton!
    @IBOutlet weak var uy: MidiKeyButton!
    @IBOutlet weak var u7: MidiKeyButton!
    @IBOutlet weak var uu: MidiKeyButton!
    @IBOutlet weak var u8: MidiKeyButton!
    
    @IBOutlet weak var ui: MidiKeyButton!
    @IBOutlet weak var uo: MidiKeyButton!
    
    //The Second row
    @IBOutlet weak var kleft: MidiKeyButton!
    @IBOutlet weak var kright: MidiKeyButton!
    
    @IBOutlet weak var kx: MidiKeyButton!
    @IBOutlet weak var kd: MidiKeyButton!
    @IBOutlet weak var kc: MidiKeyButton!
    @IBOutlet weak var kf: MidiKeyButton!
    @IBOutlet weak var kv: MidiKeyButton!
    @IBOutlet weak var kb: MidiKeyButton!
    @IBOutlet weak var kh: MidiKeyButton!
    @IBOutlet weak var kn: MidiKeyButton!
    @IBOutlet weak var kj: MidiKeyButton!
    @IBOutlet weak var km: MidiKeyButton!
    @IBOutlet weak var kk: MidiKeyButton!
    @IBOutlet weak var k_comma: MidiKeyButton!
    @IBOutlet weak var k_dot: MidiKeyButton!
    
    
    let keyboardUpCollection : [MidiKeyButton]
    let keyboardDownCollection: [MidiKeyButton]
    
    //The Second row
    
    let keyboardCharacterUp   : [Character]!
    let keyboardCharacterDown : [Character]!
    
    let keyboardCharacterUp_Uppercase : [Character]!
    let keyboardCharacterDown_Uppercase : [Character]!
    
    required init?(coder: NSCoder) {
        
        keyboardUpCollection = [uw,u3,ue,u4,ur,ut,u6,uy,u7,uu,u8,ui,uo]
        keyboardDownCollection = [kx,kd,kc,kf,kv,kb,kh,kn,kj,km,kk,k_comma,k_dot]
        
        keyboardEvent = NSEvent()
        keyboardCharacterDown = ["x","d","c","f","v","b","h","n","j","m","k",",","."]
        keyboardCharacterUp   = ["w","3","e","4","r","t","6","y","7","u","8","i","o"]
        keyboardCharacterDown_Uppercase = keyboardCharacterDown.map({ String($0).uppercased().characters.first!})
        keyboardCharacterUp_Uppercase = keyboardCharacterUp.map({ String($0).uppercased().characters.first!})
        
//        player = AVMIDIPlayer(data: nil, soundBankURL: nil)
        
        super.init(coder: coder)
    }
    
    override init?(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        keyboardEvent = NSEvent()
        keyboardEvent = NSEvent()
        
        keyboardUpCollection = [uw,u3,ue,u4,ur,ut,u6,uy,u7,uu,u8,ui,uo]
        keyboardDownCollection = [kx,kd,kc,kf,kv,kb,kh,kn,kj,km,kk,k_comma,k_dot]
        
        keyboardCharacterDown = ["x","d","c","f","v","b","h","n","j","m","k",",","."]
        keyboardCharacterUp   = ["w","3","e","4","r","t","6","y","7","u","8","i","o"]
        
        keyboardCharacterDown_Uppercase = keyboardCharacterDown.map({ String($0).uppercased().characters.first!})
        keyboardCharacterUp_Uppercase = keyboardCharacterUp.map({ String($0).uppercased().characters.first!})

       
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    override func keyDown(with event: NSEvent) {
        if let ch = event.charactersIgnoringModifiers?.characters.first {
            if keyboardCharacterUp.contains(ch) {
                
            }else if keyboardCharacterDown.contains(ch){
                
            }
        }
    }
    
    override func keyUp(with event: NSEvent) {
        if let ch = event.charactersIgnoringModifiers?.characters.first {
            if keyboardCharacterUp.contains(ch) {
                
            }else if keyboardCharacterDown.contains(ch){
                
            }
        }
    }
    
    //let m = MIDINoteMessage(channel: 1, note: 36, velocity: 10, releaseVelocity: 10, duration: 100)

    
    override func viewDidLoad() {
        
        
        
        
        
    }
    
    
    
    override func viewDidDisappear() {
        
    }
    


}
